package screens
{
	import flash.net.navigateToURL;
	import flash.net.URLRequest;
	import objects.Star_Effect;
	import starling.core.Starling;
	import starling.display.Button;
	import starling.display.MovieClip;
	import starling.display.Sprite;
	import database;
	import flash.geom.Point;
	import flash.utils.getTimer;
	import objects.BaseBuild;
	import objects.BaseBuild;
	import objects.Canon;
	import objects.Enemy;
	import objects.MyObject;
	import objects.Path;
	import objects.Tower;
	import starling.display.Image;
	import starling.display.Quad;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.extensions.PDParticleSystem;
	import starling.filters.BlurFilter;
	import starling.text.TextField;
	import starling.textures.Texture;
	import starling.utils.VAlign;
	import starling.utils.HAlign;
	import events.NavigationEvent;
	import starling.events.TouchPhase;
	import starling.filters.BlurFilter;
	import objects.ManagerEffect;
	import flash.external.ExternalInterface;
	/**
	 * ...
	 * @author ...
	 */
	public class Question extends Sprite
	{
		public var m_ImageButtonBack:Image;
		private var ScoreText:TextField;
		private var m_correctText:TextField;
		private var Score:int;
		private var m_ScoreNeed:int;
		private var m_correct:int;
		private var m_testUpLevel:Boolean;
		private var m_correctNeed:int;
		private var QuestionText:TextField;
		private var KyText:TextField;
		private var corectText:String;
		private var timeAns:int;
		
		private var m_Array_AnsTex:Array;
		private var m_Array_AnsBtn:Array;
		
		private var m_timeCoolDown:int;
		private var m_timeText:TextField;
		private var m_background:Image;
		private var timeCurrent:Number;
		private var m_countOfQuesiton:int;
		private var m_testChooseAns:Boolean;
		private var m_ansText:String;
		public var m_testStart:Boolean;
		public var m_testInit:Boolean;
		public var index:int;
		
		public var m_SpriteText:Sprite;
		public var m_backText:MovieClip;
		public var m_textBaseGT:String;
		public var m_textShowGT:TextField;
		public var m_textBaseNV:String;
		public var m_textShowNV:TextField;
		public var m_textCountNV:int;
		public var m_textCountGT:int;
		
		public var m_nextButton:Image;
		public var m_textNext:TextField;
		
		public var m_backButton:Image;
		public var m_textBack:TextField;
		
		public var m_infoButton:Image;
		public var m_textInfo:TextField;
		public var m_startQues:int;
		public var m_test_Test:Boolean;
		public var m_test_Gold:Boolean;
		
		public var m_winSprite:Sprite;
		public var m_winTextTitle:TextField;
		public var m_winTextButton:TextField;
		public var m_winButton:Image;
		public var m_winTextButtonNext:TextField;
		public var m_winButtonNext:Image;
		public var m_winBackGround:Image;
		
		public var m_loseSprite:Sprite;
		public var m_loseTextTitle:TextField;
		public var m_loseTextButton:TextField;
		public var m_loseButton:Image;
		public var m_loseTextButtonReTry:TextField;
		public var m_loseButtonReTry:Image;
		public var m_loseBackGround:Image;
		
		public var _textGioiThieu:TextField;
		public var _textNhiemVu:TextField;
		public var m_rung:int;
		public var m_rung1:int;
		public var m_rung2:int;
		public var m_rung3:int;
		public var m_rung4:int;
		public var m_rung5:int;
		public var m_rung6:int;
		public var m_rung7:int;
		public var m_rung8:int;
		public var m_rung9:int;
		public function Question()
		{
			this.addEventListener(Event.ENTER_FRAME, update);
			this.addEventListener(TouchEvent.TOUCH, onTouch);
		}
		
		private function onTouch(e:TouchEvent):void
		{
			var touch:Touch = e.getTouch(stage);
			if (touch && m_testStart == true)
			{
				var m_touchPoint:Point = new Point(touch.globalX, touch.globalY);
				if (touch.phase == TouchPhase.BEGAN)
				{
					if (m_ImageButtonBack.bounds.containsPoint(m_touchPoint) == true)
					{
						if (database.instance.m_testPlaySound == true)
							database.instance.m_Sound.playSound("Sound_Click");
						this.dispatchEvent(new NavigationEvent(NavigationEvent.CHANGE_SCREEN, {id: "Test-ChooseQuestion"}, true));
					}
					if (m_winButton.bounds.containsPoint(m_touchPoint) == true && m_winSprite.visible == true)
					{
						if (database.instance.m_testPlaySound == true)
							database.instance.m_Sound.playSound("Sound_Click");
						this.dispatchEvent(new NavigationEvent(NavigationEvent.CHANGE_SCREEN, {id: "Test-ChooseQuestion"}, true));
					}
					if (m_winButtonNext.bounds.containsPoint(m_touchPoint) == true && m_winSprite.visible == true)
					{
						if (database.instance.m_testPlaySound == true)
							database.instance.m_Sound.playSound("Sound_Click");
						if (database.instance.RESULTSTAGE[2] <= 9)
						{
							if (database.instance.RESULTSTAGE[2] == 4 && m_test_Gold == true)
							{
								var ttstar:int = int(database.instance.RESULTLEVELUPDATESTAR[6]) + 2;
								database.instance.setDataTotalStar(ttstar);
								this.dispatchEvent(new NavigationEvent(NavigationEvent.CHANGE_SCREEN, {id: "Test-Test"}, true));
								m_test_Gold = false;
							}
							if (database.instance.RESULTSTAGE[2] == 8 && m_test_Gold == true)
							{
								ttstar = int(database.instance.RESULTLEVELUPDATESTAR[6]) + 5;
								database.instance.setDataTotalStar(ttstar);
								this.dispatchEvent(new NavigationEvent(NavigationEvent.CHANGE_SCREEN, {id: "Test-Test"}, true));
								m_test_Gold = false;
							}
							if (database.instance.RESULTSTAGE[2] == 9 && m_test_Gold == true)
							{
								ttstar = int(database.instance.RESULTLEVELUPDATESTAR[6]) + 10;
								database.instance.setDataTotalStar(ttstar);
								this.dispatchEvent(new NavigationEvent(NavigationEvent.CHANGE_SCREEN, {id: "Test-ChooseQuestion"}, true));
								m_test_Gold = false;
							}
							if (database.instance.RESULTSTAGE[2] != 4 && database.instance.RESULTSTAGE[2] != 8 && database.instance.RESULTSTAGE[2] != 9)
							{
								this.dispatchEvent(new NavigationEvent(NavigationEvent.CHANGE_SCREEN, {id: "Test-Test"}, true));
							}
							/*if (database.instance.RESULTSTAGE[2] == 1)
							{
								ExternalInterface.call("openFeedDialog21");
							}
							if (database.instance.RESULTSTAGE[2] == 2)
							{
								ExternalInterface.call("openFeedDialog22");
							}
							if (database.instance.RESULTSTAGE[2] == 3)
							{
								ExternalInterface.call("openFeedDialog23");
							}
							if (database.instance.RESULTSTAGE[2] == 4)
							{
								ExternalInterface.call("openFeedDialog24");
							}
							if (database.instance.RESULTSTAGE[2] == 5)
							{
								ExternalInterface.call("openFeedDialog25");
							}
							if (database.instance.RESULTSTAGE[2] == 6)
							{
								ExternalInterface.call("openFeedDialog26");
							}
							if (database.instance.RESULTSTAGE[2] == 7)
							{
								ExternalInterface.call("openFeedDialog27");
							}
							if (database.instance.RESULTSTAGE[2] == 8)
							{
								ExternalInterface.call("openFeedDialog28");
							}
							if (database.instance.RESULTSTAGE[2] == 9)
							{
								ExternalInterface.call("openFeedDialog29");
							}*/
							if (database.instance.RESULTSTAGE[2] == 4 && m_test_Gold == false)
							{
								m_winButton.visible = false;
								m_winTextButton.visible = false;
								m_test_Gold = true;
								m_winTextTitle.text = "                       Chúc mừng\n        Bạn đã vượt qua kỳ thi hương.\n        Bạn được nhận 2 ngôi sao.";
							}
							if (database.instance.RESULTSTAGE[2] == 8 && m_test_Gold == false)
							{
								m_winButton.visible = false;
								m_winTextButton.visible = false;
								m_test_Gold = true;
								m_winTextTitle.text = "                       Chúc mừng\n        Bạn đã vượt qua kỳ thi hội.\n        Bạn được nhận 5 ngôi sao.";
							}
							if (database.instance.RESULTSTAGE[2] == 9 && m_test_Gold == false)
							{
								m_winButton.visible = false;
								m_winTextButton.visible = false;
								m_test_Gold = true;
								m_winTextTitle.text = "                       Chúc mừng\n        Bạn đã là Trạng nguyên\n        Bạn được nhận 10 ngôi sao.";
							}
							
						}
					}
					if (m_loseButton.bounds.containsPoint(m_touchPoint) == true && m_loseSprite.visible == true && m_loseSprite.visible == true)
					{
						if (database.instance.m_testPlaySound == true)
							database.instance.m_Sound.playSound("Sound_Click");
						this.dispatchEvent(new NavigationEvent(NavigationEvent.CHANGE_SCREEN, {id: "Test-ChooseQuestion"}, true));
					}
					if (m_loseButtonReTry.bounds.containsPoint(m_touchPoint) == true && m_loseSprite.visible == true && m_loseSprite.visible == true)
					{
						if (database.instance.m_testPlaySound == true)
							database.instance.m_Sound.playSound("Sound_Click");
						this.dispatchEvent(new NavigationEvent(NavigationEvent.CHANGE_SCREEN, {id: "Test-Test"}, true));
					}
					if (m_nextButton.bounds.containsPoint(m_touchPoint) == true && m_SpriteText.visible == true)
					{
						if (database.instance.m_testPlaySound == true)
							database.instance.m_Sound.playSound("Sound_Click");
						m_SpriteText.visible = false;
						m_test_Test = true;
					}
					if (m_infoButton.bounds.containsPoint(m_touchPoint) == true && m_SpriteText.visible == true)
					{
						if (database.instance.m_testPlaySound == true)
							database.instance.m_Sound.playSound("Sound_Click");
						var myURL:URLRequest = new URLRequest("http://vi.wikipedia.org/wiki/Khoa_b%E1%BA%A3ng_Vi%E1%BB%87t_Nam");
						navigateToURL(myURL, "_blank");
						m_infoButton.filter = null;
					}
					if (m_backButton.bounds.containsPoint(m_touchPoint) == true && m_SpriteText.visible == true)
					{
						if (database.instance.m_testPlaySound == true)
							database.instance.m_Sound.playSound("Sound_Click");
						m_test_Test = false;
						m_testStart = false;
						m_textCountGT = 0;
						m_textShowGT.text = "";
						m_textCountNV = 0;
						m_textShowNV.text = "";
						this.dispatchEvent(new NavigationEvent(NavigationEvent.CHANGE_SCREEN, {id: "Test-ChooseQuestion"}, true));
					}
					
				}
				else if (touch.phase == TouchPhase.HOVER)
				{
					if (m_ImageButtonBack.bounds.containsPoint(m_touchPoint) == true)
					{
						m_ImageButtonBack.filter = BlurFilter.createDropShadow(0, 0, 0xffffff, 1, 1, 0.5);
					}
					else
						m_ImageButtonBack.filter = null;
					if (m_nextButton.bounds.containsPoint(m_touchPoint) == true)
					{
						m_nextButton.filter = BlurFilter.createDropShadow(0, 0, 0xffffff, 1, 1, 0.5);
					}
					else
						m_nextButton.filter = null;
					
					if (m_infoButton.bounds.containsPoint(m_touchPoint) == true)
					{
						m_infoButton.filter = BlurFilter.createDropShadow(0, 0, 0xffffff, 1, 1, 0.5);
					}
					else
						m_infoButton.filter = null;
					
					if (m_backButton.bounds.containsPoint(m_touchPoint) == true)
					{
						m_backButton.filter = BlurFilter.createDropShadow(0, 0, 0xffffff, 1, 1, 0.5);
					}
					else
						m_backButton.filter = null;
					if (m_loseTextButton.bounds.containsPoint(m_touchPoint) == true)
					{
						m_loseTextButton.filter = BlurFilter.createDropShadow(0, 0, 0xffffff, 1, 1, 0.5);
					}
					else
						m_loseTextButton.filter = null;
					if (m_winButtonNext.bounds.containsPoint(m_touchPoint) == true)
					{
						m_winButtonNext.filter = BlurFilter.createDropShadow(0, 0, 0xffffff, 1, 1, 0.5);
					}
					else
						m_winButtonNext.filter = null;
					if (m_winButton.bounds.containsPoint(m_touchPoint) == true)
					{
						m_winButton.filter = BlurFilter.createDropShadow(0, 0, 0xffffff, 1, 1, 0.5);
					}
					else
						m_winButton.filter = null;
					if (m_loseButton.bounds.containsPoint(m_touchPoint) == true)
					{
						m_loseButton.filter = BlurFilter.createDropShadow(0, 0, 0xffffff, 1, 1, 0.5);
					}
					else
						m_loseButton.filter = null;
					if (m_loseButtonReTry.bounds.containsPoint(m_touchPoint) == true)
					{
						m_loseButtonReTry.filter = BlurFilter.createDropShadow(0, 0, 0xffffff, 1, 1, 0.5);
					}
					else
						m_loseButtonReTry.filter = null;
					
				}
			}
		}
		
		private function update(e:Event):void
		{
			ManagerEffect.instance.update(Global.FRAME);
			if (m_testInit == true)
			{
				init();
				setDefaultSprite();
				//database.instance.setStage(database.instance.RESULTSTAGE[1], 2);
				m_testInit = false;
				m_testStart = true;
				
			}
			if (m_testStart == true)
			{
				ZoomSprite(this);
				//if (this.scaleX == 1)
				//{
				//	m_rung = rung(this, m_rung);
				//}
				KyText.visible = false;
				m_timeText.visible = false;
				ScoreText.visible = false;
				m_correctText.visible = false;
				QuestionText.visible = false;
				for (i = 0; i < m_Array_AnsTex.length; i++)
				{
					m_Array_AnsTex[i].visible = false;
				}
				if (m_backText.currentFrame == 4)
				{
					m_textShowGT.visible = true;
					m_textShowNV.visible = true;
					m_backButton.visible = true;
					m_infoButton.visible = true;
					m_nextButton.visible = true;
					_textGioiThieu.visible = true;
					_textNhiemVu.visible = true;
					m_textNext.visible = true;
					m_textBack.visible = true;
					m_textInfo.visible = true;
					
					ZoomSprite(m_backButton);
					ZoomSprite(m_nextButton);
					ZoomSprite(m_infoButton);
					if (m_backButton.scaleX == 1)
					{
						m_rung1 = rung(m_backButton,m_rung1,1);
						m_rung2 = rung(m_nextButton, m_rung2,1);
						m_rung3 = rung(m_infoButton, m_rung3,1);
					}
				}
				else
				{
					m_textShowGT.visible = false;
					m_textShowNV.visible = false;
					m_backButton.visible = false;
					m_infoButton.visible = false;
					m_nextButton.visible = false;
					_textGioiThieu.visible = false;
					_textNhiemVu.visible = false;
					m_textNext.visible = false;
					m_textBack.visible = false;
					m_textInfo.visible = false;
				}
				
				if (m_SpriteText.visible == true)
				{
					if (m_backText.currentFrame >= 4)
					{
						m_backText.stop();
						m_backText.currentFrame = 4;
					}
					else
						m_backText.play();
				}
				
				m_startQues = database.instance.RESULTSTAGE[2] * 10;
				if (m_SpriteText.visible == true && m_textBaseGT != m_textShowGT.text)
				{
					m_textShowGT.text = m_textBaseGT.substring(0, m_textCountGT);
					m_textCountGT += 3;
				}
				if (m_SpriteText.visible == true && m_textBaseNV != m_textShowNV.text)
				{
					m_textShowNV.text = m_textBaseNV.substring(0, m_textCountNV);
					m_textCountNV += 3;
				}
				
			}
			if (m_test_Test == true)
			{
				KyText.visible = true;
				m_timeText.visible = true;
				ScoreText.visible = true;
				m_correctText.visible = true;
				QuestionText.visible = true;
				for (i = 0; i < m_Array_AnsTex.length; i++)
				{
					m_Array_AnsTex[i].visible = true;
				}
				timeCurrent += 1 / 60;
				if (timeCurrent >= 1)
				{
					trace(timeCurrent);
					m_timeCoolDown -= 1;
					if (m_timeCoolDown < 10)
					{
						m_timeText.x = 510;
					}
					else
						m_timeText.x = 490;
					if (m_timeCoolDown < 0)
					{
						m_timeCoolDown = 15;
						m_timeText.x = 490;
						m_countOfQuesiton += 1;
						if (m_countOfQuesiton + 1 > 10)
							m_test_Test = false;
						m_testChooseAns = false;
						m_ansText = "";
						
					}
					if (m_timeCoolDown == 0)
					{
						if (corectText == "a")
						{
							m_Array_AnsBtn[0].filter = BlurFilter.createDropShadow(0, 0, 0x00FF66, 1, 1, 0.5);
						}
						if (corectText == "b")
						{
							m_Array_AnsBtn[1].filter = BlurFilter.createDropShadow(0, 0, 0x00FF66, 1, 1, 0.5);
						}
						if (corectText == "c")
						{
							m_Array_AnsBtn[2].filter = BlurFilter.createDropShadow(0, 0, 0x00FF66, 1, 1, 0.5);
						}
						if (corectText == "d")
						{
							m_Array_AnsBtn[3].filter = BlurFilter.createDropShadow(0, 0, 0x00FF66, 1, 1, 0.5);
						}
						if (m_ansText == corectText)
						{
							ManagerEffect.instance.AddEff(new Star_Effect(ManagerEffect.instance.m_layer, new PDParticleSystem(XML(new AssetsParticles.ParticleXML1()), Texture.fromBitmap(new AssetsParticles.ParticleTexture3())), new Point(this.x, this.y), 50));
							m_correct += 1;
							Score += timeAns;
							ScoreText.text = "Điểm: " + Score;
							m_correctText.text = "Số câu đúng: " + m_correct;
							
						}
						else
						{
							ManagerEffect.instance.AddEff(new Star_Effect(ManagerEffect.instance.m_layer, new PDParticleSystem(XML(new AssetsParticles.ParticleXML2()), Texture.fromBitmap(new AssetsParticles.ParticleTexture2())), new Point(this.x, this.y), 50));
						}
					}
					timeCurrent = 0;
				}
				
				if (m_testChooseAns == false)
				{
					for (var i:int = 0; i < 4; i++)
					{
						m_Array_AnsBtn[i].filter = null;
					}
				}
				m_timeText.text = "" + m_timeCoolDown;
				index = database.instance.RESULDARRAYHUONGTEST.length / 6;
				
				if (database.instance.RESULDARRAYHUONGTEST != null)
				{
					QuestionText.text = "Câu " + (m_countOfQuesiton + 1) + " :" + database.instance.RESULDARRAYHUONGTEST[m_countOfQuesiton + m_startQues];
					m_Array_AnsTex[0].text = " " + database.instance.RESULDARRAYHUONGTEST[m_countOfQuesiton + 1 + index * 1 + m_startQues - 1];
					m_Array_AnsTex[1].text = " " + database.instance.RESULDARRAYHUONGTEST[m_countOfQuesiton + 1 + index * 2 + m_startQues - 1];
					m_Array_AnsTex[2].text = " " + database.instance.RESULDARRAYHUONGTEST[m_countOfQuesiton + 1 + index * 3 + m_startQues - 1];
					m_Array_AnsTex[3].text = " " + database.instance.RESULDARRAYHUONGTEST[m_countOfQuesiton + 1 + index * 4 + m_startQues - 1];
					corectText = "" + database.instance.RESULDARRAYHUONGTEST[m_countOfQuesiton + 1 + index * 5 + m_startQues - 1];
				}
			}
			if (m_countOfQuesiton + 1 > 10)
			{
				m_test_Test == false;
				KyText.visible = false;
				m_timeText.visible = false;
				ScoreText.visible = false;
				m_correctText.visible = false;
				QuestionText.visible = false;
				for (i = 0; i < m_Array_AnsTex.length; i++)
				{
					m_Array_AnsTex[i].visible = false;
				}
				for (i = 0; i < m_Array_AnsTex.length; i++)
				{
					m_Array_AnsTex[i].visible = false;
					m_Array_AnsBtn[i].visible = false;
				}
				if (m_correct >= m_correctNeed && Score >= m_ScoreNeed)
				{
					m_winSprite.visible = true;
					
					ZoomSprite(m_winSprite);
					if (m_winSprite.scaleX == 1)
					{
						ZoomSprite(m_winButton);
						ZoomSprite(m_winButtonNext);
						ZoomSprite(m_winTextButton);
						ZoomSprite(m_winTextButtonNext);
						if (m_winButton.scaleX == 1)
						{
							m_rung8 = rung(m_winButton, m_rung8, 1);
							m_rung9 = rung(m_winButtonNext, m_rung9,1);
						}
						m_rung5 = rung(m_winSprite, m_rung5,2);
					}
					if (m_testUpLevel == false)
					{
						m_winTextTitle.text = "                Đã hoàn thành kỳ: " + ((int)(database.instance.RESULTSTAGE[2] % 4) + 1) + "\n";
						m_winTextTitle.text += "                        Tổng kết\n";
						m_winTextTitle.text += "                Số câu đúng: " + m_correct + " / " + m_correctNeed + "\n";
						m_winTextTitle.text += "                Số Điểm: " + Score + " / " + m_ScoreNeed + "\n";
						
						if ((int)(database.instance.RESULTSTAGE[2] % 4) + 1 == 4)
						{
							database.instance.m_curQuestion += 1;
						}
						if (database.instance.RESULTSTAGE[2] <= 8)
						{
							database.instance.RESULTSTAGE[2] = int(database.instance.RESULTSTAGE[2]) + 1;
							database.instance.setStage(database.instance.RESULTSTAGE[1], database.instance.RESULTSTAGE[2]);
						}
						m_testUpLevel = true;
					}
					
				}
				else
				{
					m_loseSprite.visible = true;
					m_loseTextTitle.text = "         Không hoàn thành yêu cầu kỳ " + ((int)(database.instance.RESULTSTAGE[2] % 4) + 1) + "\n";
					m_loseTextTitle.text += "                        Tổng kết\n";
					m_loseTextTitle.text += "                Số câu đúng: " + m_correct + " / " + m_correctNeed + "\n";
					m_loseTextTitle.text += "                Số Điểm: " + Score + " / " + m_ScoreNeed + "\n";
					
					ZoomSprite(m_loseSprite);
					if (m_loseSprite.scaleX == 1)
					{
						ZoomSprite(m_loseButton);
						ZoomSprite(m_loseTextButton);
						ZoomSprite(m_loseButtonReTry);
						ZoomSprite(m_loseTextButtonReTry);
						if (m_loseButton.scaleX == 1)
						{
							m_rung6 = rung(m_loseButton, m_rung6, 1);
							m_rung7 = rung(m_loseButtonReTry, m_rung7,1);
						}
						m_rung4 = rung(m_loseSprite, m_rung4,3);
					}
				}
				
			}
		}
		
		protected function init():void
		{
			ManagerEffect.instance.m_layer = this;
			m_rung = 5;
			m_rung1 = 5;
			m_rung2 = 5;
			m_rung3 = 5;
			m_rung4 = 5;
			m_rung5 = 5;
			m_rung6 = 5;
			m_rung7 = 5;
			m_rung8 = 5;
			m_rung9 = 5;
			m_correct = 0;
			Score = 0;
			m_testUpLevel = false;
			m_Array_AnsBtn = new Array();
			m_Array_AnsTex = new Array();
			m_countOfQuesiton = 0;
			m_timeCoolDown = 15;
			timeCurrent = 0;
			m_ansText = "";
			m_SpriteText = new Sprite();
			m_winSprite = new Sprite();
			m_loseSprite = new Sprite();
			
			m_ImageButtonBack = new Image(Assets.getTexture("CHUAHOANTHANH"));
			m_ImageButtonBack.pivotX = m_ImageButtonBack.width / 2;
			m_ImageButtonBack.pivotY = m_ImageButtonBack.height / 2;
			m_ImageButtonBack.width = 50;
			m_ImageButtonBack.height = 50;
			m_ImageButtonBack.x = 1025;
			m_ImageButtonBack.y = 25;
			
			m_background = new Image(Assets.getTexture("background5"));
			this.addChild(m_background);
			m_background.scaleX = 1050 / m_background.width;
			m_background.scaleY = 700 / m_background.height;
			
			m_timeText = new TextField(150, 100, "", "MyFontName", 100, 0x332616);
			m_timeText.x = 490;
			m_timeText.y = 200;
			m_timeText.hAlign = "left";
			this.addChild(m_timeText);
			
			KyText = new TextField(500, 50, "", "MyFontName", 35, 0x332616);
			setCenterPivot(KyText);
			setPos(KyText, m_background.width/2, 100);
			this.addChild(KyText);
			
			ScoreText = new TextField(150, 50, "", "MyFontName", 25, 0x332616);
			setCenterPivot(ScoreText);
			setPos(ScoreText, m_background.width / 2 - 100, 150);
			this.addChild(ScoreText);
			
			m_correctText = new TextField(250, 50, "", "MyFontName", 25, 0x332616);
			setCenterPivot(m_correctText);
			setPos(m_correctText, m_background.width / 2 + 100, 150);
			this.addChild(m_correctText);
			
			QuestionText = new TextField(500, 100, "", "MyFontName", 25, 0x332616);
			setCenterPivot(QuestionText);
			QuestionText.x = 500;
			QuestionText.y = 420;
			QuestionText.hAlign = "left";
			this.addChild(QuestionText);
			
			for (var i:int = 0; i < 4; i++)
			{
				var _Text:TextField;
				_Text = new TextField(600, 50, "", "MyFontName", 24, 0x332616);
				_Text.x = 300;
				_Text.y = 450 + 50 * i;
				_Text.hAlign = "left";
				_Text.vAlign = "top";
				this.addChild(_Text);
				m_Array_AnsTex.push(_Text);
				
				var _Button:Button;
				_Button = new Button(Assets.getTexture("Button" + i));
				_Button.scaleX = 0.5;
				_Button.scaleY = 0.5;
				_Button.x = _Text.x - 50;
				_Button.y = _Text.y + 2;
				this.addChild(_Button);
				m_Array_AnsBtn.push(_Button);
				
			}
			index = database.instance.RESULDARRAYHUONGTEST.length / 6;
			if (database.instance.RESULDARRAYHUONGTEST != null)
			{
				QuestionText.text = "Câu " + (m_countOfQuesiton + 1) + " :" + database.instance.RESULDARRAYHUONGTEST[m_countOfQuesiton + m_startQues];
				m_Array_AnsTex[0].text = " " + database.instance.RESULDARRAYHUONGTEST[m_countOfQuesiton + index * 1 + m_startQues - 1];
				m_Array_AnsTex[1].text = " " + database.instance.RESULDARRAYHUONGTEST[m_countOfQuesiton + index * 2 + m_startQues - 1];
				m_Array_AnsTex[2].text = " " + database.instance.RESULDARRAYHUONGTEST[m_countOfQuesiton + index * 3 + m_startQues - 1];
				m_Array_AnsTex[3].text = " " + database.instance.RESULDARRAYHUONGTEST[m_countOfQuesiton + index * 4 + m_startQues - 1];
				corectText = "" + database.instance.RESULDARRAYHUONGTEST[m_countOfQuesiton + index * 5 + m_startQues - 1];
			}
			

			this.addEventListener(Event.TRIGGERED, onMainMenuClick);
			ScoreText.text = "Điểm: " + Score;
			m_correctText.text = "Số câu đúng: " + m_correct;
			
			this.addChild(m_SpriteText);
			m_backText = new MovieClip(Assets.getSpriteBook().getTextures("sachcuon_"), 10);
			Starling.juggler.add(m_backText);
			m_SpriteText.addChild(m_backText);
			m_backText.pivotX = m_backText.width / 2;
			m_backText.pivotY = m_backText.height / 2;
			
			m_backText.x = 1050 / 2;
			m_backText.y = 700 / 2;
			
			m_nextButton = new Image(Assets.getTexture("Khung"));
			m_backButton = new Image(Assets.getTexture("Khung"));
			m_infoButton = new Image(Assets.getTexture("Khung"));
			m_textNext = new TextField(500, 50, "Tham gia", "MyFontName", 20, 0x332616);
			m_textBack = new TextField(500, 50, "Bỏ thi", "MyFontName", 20, 0x332616);
			m_textInfo = new TextField(500, 50, "Thông tin", "MyFontName", 20, 0x332616);
			
			m_nextButton.pivotX = m_nextButton.width / 2;
			m_nextButton.pivotY = m_nextButton.height / 2;
			m_nextButton.x = 800;
			m_nextButton.y = 520;
			setCenterPivot(m_textNext);
			m_textNext.x = m_nextButton.x;
			m_textNext.y = m_nextButton.y;
			
			m_infoButton.pivotX = m_infoButton.width / 2;
			m_infoButton.pivotY = m_infoButton.height / 2;
			m_infoButton.x = 520;
			m_infoButton.y = 520;
			setCenterPivot(m_textInfo);
			m_textInfo.x = m_infoButton.x ;
			m_textInfo.y = m_infoButton.y;
			
			m_backButton.pivotX = m_backButton.width / 2;
			m_backButton.pivotY = m_backButton.height / 2;
			m_backButton.x = 240;
			m_backButton.y = 520;
			setCenterPivot(m_textBack);
			m_textBack.x = m_backButton.x ;
			m_textBack.y = m_backButton.y ;
			
			m_textShowGT = new TextField(300, 450, "", "MyFontName", 20, 0x332616);
			m_textShowGT.hAlign = "left";
			m_textShowGT.vAlign = "top";
			m_textShowGT.x = m_backText.x - 350;
			m_textShowGT.y = m_backText.y - 100;
			
			m_textShowNV = new TextField(300, 450, "", "MyFontName", 20, 0x332616);
			m_textShowNV.hAlign = "left";
			m_textShowNV.vAlign = "top";
			m_textShowNV.x = m_backText.x + 50;
			m_textShowNV.y = m_backText.y - 100;
			
			_textGioiThieu = new TextField(300, 100, "Giới thiệu", "MyFontName", 30, 0x332616);
			_textNhiemVu = new TextField(300, 100, "Nhiệm Vụ", "MyFontName", 30, 0x332616);
			
			setCenterPivot(_textGioiThieu);
			_textGioiThieu.x = m_backText.x- 230;
			_textGioiThieu.y = m_backText.y - 120;
			setCenterPivot(_textNhiemVu);
			_textNhiemVu.x = m_backText.x + 160;
			_textNhiemVu.y = m_backText.y - 120;
			
			m_SpriteText.addChild(m_textShowGT);
			m_SpriteText.addChild(m_textShowNV);
			m_SpriteText.addChild(m_nextButton);
			m_SpriteText.addChild(m_infoButton);
			m_SpriteText.addChild(m_backButton);
			m_SpriteText.addChild(m_textNext);
			m_SpriteText.addChild(m_textBack);
			m_SpriteText.addChild(m_textInfo);
			m_SpriteText.addChild(_textGioiThieu);
			m_SpriteText.addChild(_textNhiemVu);
			
			m_textShowGT.visible = false;
			m_textShowNV.visible = false;
			m_backButton.visible = false;
			m_infoButton.visible = false;
			m_nextButton.visible = false;
			_textGioiThieu.visible = false;
			_textNhiemVu.visible = false;
			m_textNext.visible = false;
			m_textBack.visible = false;
			m_textInfo.visible = false;
			
			m_SpriteText.visible = true;
			
			m_textCountGT = 0;
			m_textShowGT.text = "";
			m_textCountNV = 0;
			m_textShowNV.text = "";
			
			setSize(m_winSprite, 1050, 700);
			setCenterPivotXY(m_winSprite, 1050 / 2, 700 / 2);
			setPos(m_winSprite, 1050 / 2, 700 / 2);
			setScale(m_winSprite, 0.1, 0.1);
			
			//khoi tao win
			m_winTextTitle = new TextField(500, 200, "", "MyFontName", 30, 0x332616);
			m_winTextButton = new TextField(100, 50, "Trở về", "MyFontName", 25, 0x332616);
			m_winButton = new Image(Assets.getTexture("Khung"));
			m_winBackGround = new Image(Assets.getTexture("Khungwin"));
			
			m_winTextButtonNext = new TextField(100, 50, "Tiếp tục", "MyFontName", 25, 0x332616);
			m_winButtonNext = new Image(Assets.getTexture("Khung"));
			
			m_winTextTitle.hAlign = "left";
			setCenterPivot(m_winTextTitle);
			setPos(m_winTextTitle, 1050 / 2, 300);
			
			setCenterPivot(m_winButton);
			setPos(m_winButton, 1050 / 2 - 100, 700 / 2 + 50);
			
			setCenterPivot(m_winTextButton);
			setPos(m_winTextButton, m_winButton.x , m_winButton.y);
			
			setCenterPivot(m_winButtonNext);
			setPos(m_winButtonNext, 1050 / 2 + 100, 700 / 2 + 50);
			
			setCenterPivot(m_winTextButtonNext);
			setPos(m_winTextButtonNext, m_winButtonNext.x, m_winButtonNext.y);
			
			setCenterPivot(m_winBackGround);
			setPos(m_winBackGround, 1050 / 2, 700 / 2);
			
			
			
			m_winSprite.addChild(m_winBackGround);
			m_winSprite.addChild(m_winTextTitle);
			m_winSprite.addChild(m_winButton);
			m_winSprite.addChild(m_winTextButton);
			m_winSprite.addChild(m_winButtonNext);
			m_winSprite.addChild(m_winTextButtonNext);
			
			this.addChild(m_winSprite);
			m_winSprite.visible = false;
			
			//khoi tao lose
			
			setSize(m_loseSprite, 1050, 700);
			setCenterPivotXY(m_loseSprite, 1050 / 2, 700 / 2);
			setPos(m_loseSprite, 1050 / 2, 700 / 2);
			setScale(m_loseSprite, 0.1, 0.1);
			
			m_loseTextTitle = new TextField(500, 200, "", "MyFontName", 30, 0x332616);
			m_loseBackGround = new Image(Assets.getTexture("Khungwin"));
			
			m_loseTextButton = new TextField(100, 50, "Trở về", "MyFontName", 25, 0x332616);
			m_loseButton = new Image(Assets.getTexture("Khung"));
			
			m_loseTextButtonReTry = new TextField(100, 50, "Thử lại", "MyFontName", 25, 0x332616);
			m_loseButtonReTry = new Image(Assets.getTexture("Khung"));
			
			m_loseTextTitle.hAlign = "left";
			setCenterPivot(m_loseTextTitle);
			setPos(m_loseTextTitle, 1050 / 2, 300);
			
			setCenterPivot(m_loseButton);
			setPos(m_loseButton, m_loseSprite.x-100, m_loseSprite.y+50);
			
			setCenterPivot(m_loseTextButton);
			setPos(m_loseTextButton, m_loseButton.x , m_loseButton.y);
			
			setCenterPivot(m_loseButtonReTry);
			setPos(m_loseButtonReTry, m_loseSprite.x+ 100, m_loseSprite.y+50);
			
			setCenterPivot(m_loseTextButtonReTry);
			setPos(m_loseTextButtonReTry, m_loseButtonReTry.x , m_loseButtonReTry.y);
			
			setCenterPivot(m_loseBackGround);
			setPos(m_loseBackGround, 1050 / 2, 700 / 2);
			
			
			m_loseSprite.addChild(m_loseBackGround);
			m_loseSprite.addChild(m_loseTextTitle);
			m_loseSprite.addChild(m_loseButton);
			m_loseSprite.addChild(m_loseTextButton);
			m_loseSprite.addChild(m_loseButtonReTry);
			m_loseSprite.addChild(m_loseTextButtonReTry);
			
			this.addChild(m_loseSprite);
			m_loseSprite.visible = false;
			
			m_correctNeed = 0;
			switch (int(database.instance.RESULTSTAGE[2]))
			{
				case 0: 
					m_ScoreNeed = 25;
					m_correctNeed = 5;
					break;
				case 1: 
					m_ScoreNeed = 30;
					m_correctNeed = 5;
					break;
				case 2: 
					m_ScoreNeed = 35;
					m_correctNeed = 5;
					break;
				case 3: 
					m_ScoreNeed = 40;
					m_correctNeed = 5;
					break;
				case 4: 
					m_ScoreNeed = 45;
					m_correctNeed = 6;
					break;
				case 5: 
					m_ScoreNeed = 50;
					m_correctNeed = 6;
					break;
				case 6: 
					m_ScoreNeed = 55;
					m_correctNeed = 7;
					break;
				case 7: 
					m_ScoreNeed = 60;
					m_correctNeed = 7;
					break;
				case 8: 
					m_ScoreNeed = 65;
					m_correctNeed = 8;
					break;
			}
			switch (database.instance.m_curQuestion)
			{
				case 0: 
					KyText.text = "Thi hương vòng " + ((int)(database.instance.RESULTSTAGE[2] % 4) + 1);
					m_textBaseGT = "Thi Hương là một khóa thi cử về nho học do triều đình phong kiến tổ chức để tuyển chọn người có tài ở các địa phương. Kỳ thi Hương là kỳ thi sơ khởi nhất. Sau khi đỗ thi Hương thì năm sau mới được dự thi kỳ thi cao cấp hơn.\nĐỗ đầu kỳ thi Hương gọi là Giải nguyên.\nTheo quy định từ năm 1434, thi Hương có 4 kỳ.";
					m_textBaseNV = "Vượt qua vòng " + ((int)(database.instance.RESULTSTAGE[2] % 4) + 1) + ".\n- Đạt được: " + m_ScoreNeed + " điểm.\n- Trả lời đúng: " + m_correctNeed + " câu.";
					break;
				case 1: 
					KyText.text = "Thi hội vòng " + ((int)(database.instance.RESULTSTAGE[2] % 4) + 1);
					m_textBaseGT = "Thi Hội là một khóa thi cử về Nho học do bộ Lễ của triều đình phong kiến tổ chức 3 năm một lần tại các trường trung ương để tuyển chọn người có tài, học rộng.Sau khi đỗ thi Hội thì năm sau mới được dự thi kỳ thi Đình.\nĐỗ đầu kỳ thi Hội gọi là Hội nguyên.\nTheo quy định từ năm 1434, thi Hội có 4 kỳ";
					m_textBaseNV = "Vượt qua vòng " + ((int)(database.instance.RESULTSTAGE[2] % 4) + 1) + ".\n- Đạt được: " + m_ScoreNeed + " điểm.\n- Trả lời đúng: " + m_correctNeed + " câu.";
					break;
				case 2: 
					KyText.text = "Thi đình";
					m_textBaseGT = "Thi Đình là một khóa thi cử về nho học cao cấp nhất do triều đình phong kiến tổ chức để tuyển chọn người có tài, học rộng. Người thi đỗ được cấp bằng và có thể nhờ đó mà được vào làm quan chức trong triều chính.\nĐỗ đầu thi Đình gọi là Đình nguyên hay Điện nguyên.\nGọi là thi Đình vì thi trong điện của vua. Vua ra đề và chấm khảo thi.";
					m_textBaseNV = "Vượt qua kỳ thi cuối cùng, thi Đình\n- Đạt được: " + m_ScoreNeed + " điểm.\n- Trả lời đúng: " + m_correctNeed + " câu.";
					break;
			}
			
			this.addChild(m_ImageButtonBack);
			
			if (database.instance.m_testPlayMusic == true)
				database.instance.m_Music.playSound("Question_Music", 1.0, 999);
		
		}
		
		private function onMainMenuClick(e:Event):void
		{
			if (m_testChooseAns == false)
			{
				if (database.instance.m_testPlaySound == true)
							database.instance.m_Sound.playSound("Sound_Click");
				if ((e.target as Button == m_Array_AnsBtn[0]))
				{
					m_testChooseAns = true;
					timeAns = m_timeCoolDown;
					m_ansText = "a";
					m_Array_AnsBtn[0].filter = BlurFilter.createDropShadow(0, 0, 0xffffff, 1, 1, 0.5);
				}
				if ((e.target as Button == m_Array_AnsBtn[1]))
				{
					m_testChooseAns = true;
					timeAns = m_timeCoolDown;
					m_ansText = "b";
					m_Array_AnsBtn[1].filter = BlurFilter.createDropShadow(0, 0, 0xffffff, 1, 1, 0.5);
				}
				if ((e.target as Button == m_Array_AnsBtn[2]))
				{
					m_testChooseAns = true;
					timeAns = m_timeCoolDown;
					m_ansText = "c";
					m_Array_AnsBtn[2].filter = BlurFilter.createDropShadow(0, 0, 0xffffff, 1, 1, 0.5);
				}
				if ((e.target as Button == m_Array_AnsBtn[3]))
				{
					m_testChooseAns = true;
					timeAns = m_timeCoolDown;
					m_ansText = "d";
					m_Array_AnsBtn[3].filter = BlurFilter.createDropShadow(0, 0, 0xffffff, 1, 1, 0.5);
				}
			}
		}
		
		private function ZoomSprite(_object:Object):void
		{
			if (_object.scaleX < 1)
			{
				_object.scaleX += 0.05;
				_object.scaleY = this.scaleX;
			}
			else
			{
				_object.scaleX = 1;
				_object.scaleY = 1;
			}
		}
		public function rung(obj:Object, _rung:int,_buzz:int):int
		{
			if (_rung != 0)
				{
					if (_rung > 0)
					{
						_rung -= 1;
					}
					obj.x += _rung;
					obj.y -= _rung;
					_rung *= -1;
					if(_rung==4&&_buzz==1)
						ManagerEffect.instance.AddEff(new Star_Effect(ManagerEffect.instance.m_layer, new PDParticleSystem(XML(new AssetsParticles.ParticleXML()), Texture.fromBitmap(new AssetsParticles.ParticleTexture())), new Point(obj.x, obj.y), 50));
					if(_rung==4&&_buzz==2)
						ManagerEffect.instance.AddEff(new Star_Effect(ManagerEffect.instance.m_layer, new PDParticleSystem(XML(new AssetsParticles.ParticleXML1()), Texture.fromBitmap(new AssetsParticles.ParticleTexture1())), new Point(obj.x, obj.y), 50));
					if(_rung==4&&_buzz==3)
						ManagerEffect.instance.AddEff(new Star_Effect(ManagerEffect.instance.m_layer, new PDParticleSystem(XML(new AssetsParticles.ParticleXML2()), Texture.fromBitmap(new AssetsParticles.ParticleTexture4())), new Point(obj.x, obj.y), 50));
					return _rung;
				}
				return _rung;
		}
		private function setDefaultSprite():void
		{
			
			this.width = 1050;
			this.height = 700;
			this.pivotX = this.width / 2;
			this.pivotY = this.height / 2;
			this.x = this.width / 2;
			this.y = this.height / 2;
			this.scaleX = 0.1;
			this.scaleY = 0.1;
			setScale(m_backButton, 0, 0);
			setScale(m_infoButton, 0, 0);
			setScale(m_nextButton, 0, 0);
			
			setScale(m_loseButton, 0, 0);
			setScale(m_loseTextButton, 0, 0);
			setScale(m_loseTextButtonReTry, 0, 0);
			setScale(m_loseButtonReTry, 0, 0);
			
			setScale(m_winButton, 0, 0);
			setScale(m_winTextButton, 0, 0);
			setScale(m_winButtonNext, 0, 0);
			setScale(m_winTextButtonNext, 0, 0);
		}
		
		public function disposeTemporarily():void
		{
			m_test_Test = false;
			ManagerEffect.instance.ListEff.splice(0, ManagerEffect.instance.ListEff.length);
			database.instance.m_Music.stopSound("Question_Music");
			this.removeChildren(0, this.numChildren, false);
			this.removeChildren(0, this.numChildren, false);
			this.scaleX = 0.1;
			this.scaleY = 0.1;
			this.m_testStart = false;
			this.visible = false;
		}
		
		public function setCenterPivot(_object:Object):void
		{
			_object.pivotX = _object.width / 2;
			_object.pivotY = _object.height / 2;
		}
		
		public function setCenterPivotXY(_object:Object, _x:Number, _y:Number):void
		{
			_object.pivotX = _x;
			_object.pivotY = _y;
		}
		
		public function setPos(_object:Object, _x:int, _y:int):void
		{
			_object.x = _x;
			_object.y = _y;
		}
		
		public function setScale(_object:Object, _x:Number, _y:Number):void
		{
			_object.scaleX = _x;
			_object.scaleY = _y;
		}
		
		public function setSize(_object:Object, _x:int, _y:int):void
		{
			_object.width = _x;
			_object.height = _y;
		}
	}

}